<template>
	<view>
		<van-card @click.native="goGoodsDetail(item.goods_id)" v-for="item in goods" :key="item.goods_id"
			:price="item.goods_price.toFixed(2)" :title="item.goods_name" :thumb="item.goods_big_logo || defaultPic" />

	</view>
</template>

<script>
	import {
		loadGoodsList
	} from '@/api/goods.js'
	export default {
		data() {
			return {
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				// 获取商品列表接口的参数
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				// 商品列表
				goods: [],
				total: 0,
				isLoading: false // 添加节流阀
			};
		},
		methods: {
			async getGoodsList(cb) {
				if (this.isLoading) return
				this.isLoading = true
				const res = await loadGoodsList(this.queryObj)

				this.goods.push(...res.data.message.goods)
				this.total = res.data.message.total
				this.isLoading = false
				cb && cb()
			},
			// 跳转到商品详情
			goGoodsDetail(id) {
				uni.navigateTo({
					url: "/pages/goods_detail/goods_detail?id=" + id
				})
			}
		},
		onLoad(options) {
			if (options.query) {
				this.queryObj.query = options.query
			}
			this.getGoodsList()
		},
		// 监听触底
		onReachBottom() {
			// console.log('触底了')
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
				uni.showToast({
					title: '客观没有更多数据了呦',
					icon: 'none'
				})
				return
			}
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		// 监听下拉刷新
		onPullDownRefresh() {
			console.log('出发下拉刷新')
			// 1. 刷新页面重新获取第一页的数据
			this.goods = []
			this.queryObj.pagenum = 1
			this.getGoodsList(() => {
				uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss">

</style>
